package com.tdam2014.grupo3.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tdam2014.grupo3.R;
import com.tdam2014.grupo3.entities.Comment;

public class CommentAdapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private ArrayList<Comment> comments;
	
	public CommentAdapter(Context context) {
		this.inflater = LayoutInflater.from(context);
	}

	public void setComments(ArrayList<Comment> comments) {
		this.comments = comments;
	}

	@Override
	public int getCount() {
		return comments.size();
	}

	@Override
	public Comment getItem(int position) {
		return comments.get(position);
	}

	@Override
	public long getItemId(int position) {
		return -1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View item = null;
		HolderCommentItemList holder = null;
		
		if(convertView == null) {
			item = inflater.inflate(R.layout.comment_list_item, null);
			holder = new HolderCommentItemList();
			holder.authorname = (TextView) item.findViewById(R.id.txtCommentAuthorname);
			holder.dateCreated = (TextView) item.findViewById(R.id.txtCommentDateCreated);
			holder.text = (TextView) item.findViewById(R.id.txtCommentText);
			item.setTag(holder);
		} else {
			item = convertView;
			holder = (HolderCommentItemList) item.getTag();
		}
		
		Comment comment = comments.get(position);
		
		holder.authorname.setText(comment.getAuthorName());
		holder.dateCreated.setText(comment.getDateCreatedDateTime());
		holder.text.setText(comment.getText());

		return item;
	}
	
	private class HolderCommentItemList {
		TextView authorname;
		TextView dateCreated;
		TextView text;
	}
}
